﻿using UnityEngine;
using Holoville.HOTween;
using System.Collections;
using System;

public class TweenTextMatrix : MonoBehaviour
{
	public int speed =10;
	public int numTextEff = 5;
	UILabel label;
	[HideInInspector]
	public float desNumTemp = 0;
	void Start()
	{
		arrRan = srtRand.ToCharArray();
		lengtRand = arrRan.Length;
	}
	public void PlayEffText()
	{
		if (label == null)
		{
			label = GetComponent<UILabel>();
		}
		if (label == null)
		{
			return;
		}
		PlayEffText(label.text);
	}
	public void PlayEffText(string text)
	{
		if (tweenNum != null && tweenNum.isComplete == false)
		{
			tweenNum.Kill();
		}
		//if (ConfigManager.instance.configLocalizeStatic == null)
		//{
		//    ConfigManager.instance.Init();
		//}
		//if (ConfigManager.instance.configLocalizeStatic != null)
		//{
		//    saveText = ConfigManager.instance.configLocalizeStatic.GetCurLocalizeText(text);
		//}

		if (label == null)
		{
			label = GetComponent<UILabel>();
		}
		if (label == null)
		{
			return;		
		}
		randTail = "";
		lenght = saveText.Length;
		curLenght = 0;
		label.text = "";
		tweenNum = HOTween.To(this, speed * lenght, new TweenParms().Prop("desNumTemp", 2).OnUpdate(UpdateText).OnComplete(CompleteString));
	}
	string saveText = "";
	char[] arrRan;
	int lengtRand = 0;
	string srtRand = @"abcdefghijklmnopqrstuvwxyz/?!@#$%^&*()1234567890-=+`~<>,.ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	Tweener tweenNum;
	long curUpdateTime = 0;
	int lenght = 0;
	int curLenght = 0;
	string randTail = "";
	void UpdateText()
	{
		if (((DUtil.GetNumberOfMiliSecondsFromMilestone() - curUpdateTime) * 1000) < (long)(speed  * 1000))
		{
			return;
		}
		if (numTextEff == 0)
		{
			string srtRand = "_______";
			arrRan = srtRand.ToCharArray();
			lengtRand = arrRan.Length;
		}
		else
		{
			arrRan = srtRand.ToCharArray();
			lengtRand = arrRan.Length;
		}
		curUpdateTime = DUtil.GetNumberOfMiliSecondsFromMilestone();
		if (label == null)
		{
			label = GetComponent<UILabel>();
		}
		if (label == null)
		{
			return;
		}
		curLenght++;
		string temp = saveText.Substring(0, curLenght > lenght ? lenght : curLenght);
		if ((randTail.Length >= numTextEff || randTail.Length + temp.Length >= lenght) && (randTail.Length > 0))
		{
			randTail = randTail.Remove(0, 1);
		}
		if (randTail.Length + temp.Length < lenght)
		{
			randTail += arrRan[UnityEngine.Random.Range(0, 1000) % lengtRand];
		}
		temp += randTail;
		label.text = temp;
	}
	void CompleteString()
	{
		label.text = saveText;
	}
}